﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class DialogFieldController :UI.BaseController
    {
        public const string ProgrameCode = "Dialog";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public DialogFieldController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode , "view_field")]
        public ActionResult Index(string DialogCode)
        {
            ViewBag.DialogCode = DialogCode;
            return View();
        }

        [UI.ValidateAuthority(ProgrameCode , "view_field")]
        public string List(int page, int rows, string sort, string order)
        {
            string where = Common.HqlQeuryHelp.GetWhere(typeof(Model.DialogField), "DialogCode,FieldKey,Title");
            string orderby = "Sort desc";
            if (!string.IsNullOrEmpty(sort)) orderby = sort + " " + order;

            var bll = new BLL.DialogField();
            var list = bll.Get(where, orderby, (page - 1) * rows, rows);
            List<object> listView = new List<object>();
            if (list.Count > 0)
            {
                Model.Dialog dialog = list[0].Dialog;
                foreach (var i in list)
                {
                    listView.Add(new
                    {
                        id = i.ID,
                        FieldKey = i.FieldKey,
                        Title = i.Title,
                        Width = i.Width,
                        Sort = i.Sort,
                        IsShow = i.IsShow,
                        IsResult = i.FieldKey == dialog.ResultFieldKey
                    });
                }
            }
            var ret = new
            {
                total = bll.GetCount(where),
                rows = listView
            };
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "delete_field")]
        public string Delete(string id)
        {
            var ret = new BLL.DialogField().Delete(id);
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "add_field")]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "add_field")]
        public string Add(Model.DialogField model, bool? IsResult)
        {
            var ret = new BLL.DialogField().Add(model, IsResult);
            return UI.Utils.ToJsonString(ret);
            
        }

        [UI.ValidateAuthority(ProgrameCode , "edit_field")]
        public ActionResult Edit(int id)
        {
            var model = new BLL.DialogField().Get(id);
            if (model == null) return UI.Utils.ErrorDateNoExist();
            return View(model);
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "edit_field")]
        public string Edit(Model.DialogField model, bool? IsResult)
        {
            var ret = new BLL.DialogField().Update(model, IsResult);
            return UI.Utils.ToJsonString(ret);
        }
    }
}
